package com.sunyur.tailor.channel.web.inner.event.controller;

import com.alibaba.fastjson.JSON;
import com.sunyur.tailor.channel.common.inner.base.SunyurResult;
import com.sunyur.tailor.channel.common.middle.util.MethodNameConversionUtil;
import com.sunyur.tailor.channel.service.inner.base.SunyurEventService;
import com.sunyur.tailor.channel.web.inner.event.bean.SunyurEventContainer;
import com.sunyur.tailor.channel.web.inner.event.dto.SunyurEventDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

/**
 *
 * @Author 99288
 * @Description 商越事件
 * @Param
 * @return
**/
@RestController
@RequestMapping("/http/sunyurEvent")
public class SunyurEventController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SunyurEventController.class);

    /**
     *
     * @Author 99288
     * @Description 统一入口
     * @Param
     * @return
    **/
    @PostMapping(value = "/entrance", produces = "application/json;charset=UTF-8")
    public SunyurResult<Boolean> entrance(@RequestBody SunyurEventDTO sunyurEventDTO) {
        SunyurResult<Boolean> result = new SunyurResult<>();
        result.setContent(false);
        if (sunyurEventDTO == null) {
            LOGGER.info("SunyurEventController entrance param is null.");
            return result;
        }
        try {
            LOGGER.info("SunyurEventController entrance param:{}", JSON.toJSONString(sunyurEventDTO));
            SunyurEventService service = SunyurEventContainer.get(sunyurEventDTO.getBusiness());
            return (SunyurResult<Boolean>) service
                    .getClass()
                    .getMethod(MethodNameConversionUtil.camelName(sunyurEventDTO.getEvent()), String.class)
                    .invoke(service, JSON.toJSONString(sunyurEventDTO.getData()));
        } catch (Exception e) {
            LOGGER.error("SunyurEventController entrance exception:{}", e);
        }
        return result;
    }
}
